import { Node } from "cc";

export class Fish {
    position: number[];
    velocity: number[];
    fitness: number;
    private target:any;

    constructor(position: number[], velocity: number[],fishNode?:any) {
        this.position = position;
        this.velocity = velocity;
        this.fitness = Infinity;
        this.target = fishNode;
    }

    updateVelocity(bestFish: Fish) {
        let newVelocity = [];
        for (let i = 0; i < this.position.length; i++) {
            let r = Math.random();
            let c = 2 * r * (r - 1);
            newVelocity.push(this.velocity[i] + c * (this.position[i] - bestFish.position[i]));
        }
        this.velocity = newVelocity;
    }

    updatePosition() {
        let newPosition = [];
        for (let i = 0; i < this.position.length; i++) {
            newPosition.push(this.position[i] + this.velocity[i]);
        }
        this.position = newPosition;
        if(this.target && this.target.node){
            let posX = this.position[0];
            let posY = this.position[1];
            this.target.node.setPosition(posX,posY);
        }
    }

    calculateFitness(func: Function) {
        this.fitness = func(this.position);
    }
}